package com.sz.biz.logistics.ord.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.ProductServiceRel;
import com.sz.biz.logistics.core.service.ProductServiceRelService;
import com.sz.biz.logistics.ord.entity.OrdTrackingMessage;
import com.sz.biz.logistics.ord.enums.TrackMessageToServiceEnum;
import com.sz.biz.logistics.ord.service.OrdTrackingMessageService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.common.base.utils.ObjectUtils;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Package: com.sz.biz.logistics.ord.service.impl <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: hpeng.wang. <br>
 * Date: 2017-07-20 14:02:00.
 */
@Service
public class OrdTrackingMessageServiceImpl extends AbstractService implements OrdTrackingMessageService {
    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.logistics.ord.entity.OrdTrackingMessageMapper";
    }

    @Autowired
    private ProductServiceRelService productServiceRelService;

    @Override
    public void save(OrdTrackingMessage entry) {
        if (!ObjectUtils.isEmpty(entry)) {
            this.batchSave(Arrays.asList(entry));
        }
    }

    @Override
    public void batchSave(List<OrdTrackingMessage> list) {
        if (!CollectionUtils.isEmpty(list)) {
            dao.batchInsert(getSqlName("insertSelective"), list);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("ADD_ORDER_TRACKING_MESSAGE", list);
            PrincipalLogUtils.addOperationLog(ModuleNames.ORD, ModuleNames.ORD_ORDER_TRACKING, UserActions.ADD, "添加物流的消息信息", dataBackup);
        }

    }

    @Override
    public List<OrdTrackingMessage> findReceTracking(TrackMessageToServiceEnum eventCode) {
        List<OrdTrackingMessage> ordTrackingMessages = new ArrayList<>();
        if (!StringUtils.isEmpty(eventCode)) {
            ordTrackingMessages = dao.findForList(getSqlName("findReceTracking"), eventCode.getCode(), OrdTrackingMessage.class);
        }
        return ordTrackingMessages;
    }

    /**
     * @param waybillNo
     * @return
     */
    @Override
    public OrdTrackingMessage findCurrentTracking(String waybillNo) {
        OrdTrackingMessage trackingMessage = null;
        if (!StringUtils.isEmpty(waybillNo)) {
            trackingMessage = (OrdTrackingMessage) dao.findForObject(getSqlName("findCurrentTracking"), waybillNo);
        }
        return trackingMessage;
    }

    @Override
    public ProductServiceRel findCurServiceByTracking(String waybillNo, String productUid, Date orderCreateTime) {
        OrdTrackingMessage trackingMessage = this.findCurrentTracking(waybillNo);
        ProductServiceRel productServiceRel = null;
        if (!ObjectUtils.isEmpty(trackingMessage)) {
            String serviceUid = trackingMessage.getServiceUid();
            List<ProductServiceRel> productServiceRelList = productServiceRelService.findServicesByUidAndDate(productUid, orderCreateTime);
            for (ProductServiceRel serviceRel : productServiceRelList) {
                if (serviceUid.equals(serviceRel.getServiceUid())) {
                    productServiceRel = serviceRel;
                    break;
                }
            }
        }
        return productServiceRel;
    }


    @Override
    public void updateStatusByIds(List<Integer> ids, int status) {
        if (!CollectionUtils.isEmpty(ids)) {
            Map<Integer, Integer> idStatusMap = new HashMap<>();
            ids.forEach(id -> idStatusMap.put(id, status));
            this.batchUpdateStatusByIds(idStatusMap);
        }
    }

    @Override
    public void batchUpdateStatusByIds(Map<Integer, Integer> idStatusMap) {
        if (!CollectionUtils.isEmpty(idStatusMap)) {
            List<ParamData> pdList = new ArrayList<>();
            idStatusMap.forEach((id, status) -> {
                ParamData pd = new ParamData();
                pd.put("id", id);
                pd.put("status", status);
                pdList.add(pd);
            });
            dao.batchUpdate(getSqlName("updateStatus"), pdList);
        }
    }

    @Override
    public OrdTrackingMessage findById(Integer id) {
        return null;
    }


    @Override
    public String findServiceUidByWaybillNo(String waybillNo, int supplierId) {
        String serviceUid = null;
        if (!StringUtils.isEmpty(waybillNo) && supplierId > 0) {
            ParamData pd = new ParamData();
            pd.put("waybillNo", waybillNo);
            pd.put("supplierId", supplierId);
            serviceUid = (String) dao.findForObject(getSqlName("findServiceUidByWaybillNo"), pd);

        }
        return serviceUid;
    }

    @Override
    public List<OrdTrackingMessage> findServiceUidByWaybillNos(List<String> waybillNos, List<Integer> supplierIds) {
        List<OrdTrackingMessage> trackingMessageList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(waybillNos) && !CollectionUtils.isEmpty(supplierIds)) {
            ParamData pd = new ParamData();
            pd.put("waybillNos", waybillNos);
            pd.put("supplierIds", supplierIds);
            trackingMessageList = dao.findForList(getSqlName("findServiceUidByWaybillNos"), pd, OrdTrackingMessage.class);
        }
        return trackingMessageList;
    }

    /**
     * 根据运单号查询当前订单，最后一条阻塞的物流信息，并发返回
     *
     * @param waybillNo 订单号
     * @return 阻塞的物流信息 ，如果没有，则返回空
     */
    @Override
    public OrdTrackingMessage findBlockTracking(String waybillNo) {
        OrdTrackingMessage trackingMessage = null;
        if (!StringUtils.isEmpty(waybillNo)) {
            List<OrdTrackingMessage> trackingMessageList = this.findTrackingByWaybillNo(waybillNo, false);
            if (!CollectionUtils.isEmpty(trackingMessageList)) {
                for (OrdTrackingMessage message : trackingMessageList) {
                    if (TrackMessageToServiceEnum.MSG_BLOCK_OUTGOING.getCode().equals(message.getEventCode())) {
                        trackingMessage = message;
                    }
                }
            }
        }
        return trackingMessage;
    }

    public List<OrdTrackingMessage> findTrackingByWaybillNo(String waybillNo, Boolean asc) {
        List<OrdTrackingMessage> trackingMessageList = new ArrayList<>();
        if (!StringUtils.isEmpty(waybillNo)) {
            ParamData pd = new ParamData();
            pd.put("waybillNo", waybillNo);
            pd.put("asc", asc);
            trackingMessageList = dao.findForList(getSqlName("findTrackingByWaybillNo"), pd, OrdTrackingMessage.class);
        }
        return trackingMessageList;
    }
}
